﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports ISNet.WebUI.WebTreeView
Imports log4net

Partial Class F05
    Inherits System.Web.UI.Page

    Dim cmmtcode As String = ""
    Dim lockhash As New Hashtable

    Private Shared logger As ILog = LogManager.GetLogger("F05")

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        WebCombo3.EncodeOutput = True
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        cmmtcode = Session("CMMT_OT_CODE")
        PlanYearsTxt.Text = Session("PlanYear")
        VersionTxt.Text = Session("Versions")
        If Session("FCTR") Is Nothing Then
            Response.Redirect("Selector.aspx?type=10")
        End If
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        GenerateValues()

        If IsPostBack = False Then
            Session.Remove("Search")
            Session.Remove("search_fctr")


            Label1.Text = "ระบบจัดทำงบประมาณ >> จัดทำงบประมาณ >> ค่าใช้จ่ายที่เกี่ยวกับผลิตภัณฑ์ >> ค่าใช้จ่ายดำเนินคดีและค่าจ้างทนายความ"
            'If Session("Versions") = 1 Then
            '    div1.Visible = False
            '    div2.Visible = False
            '    SearhBtn.Visible = False
            '    WebTreeView1.Visible = False
            '    WebTreeView2.Visible = False
            '    WebTreeView3.Visible = False
            'End If
            Dim adapter As New BasicDataTableAdapters.ProductsTableAdapter
            Dim data As New BasicData.ProductsDataTable

            data = adapter.GetData(Val(PlanYearsTxt.Text))

            ms8.DataValueField = "ProductCode"
            ms8.DataTextField = "Description"
            ms8.DataSource = data
            ms8.DataBind()

        End If

        'If WebGrid1.IsFlyPostBack = False Then

        '    PlotNode()
        'End If
        Dim prefil2 As String = ""
        prefil2 = Session("prefilter")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        Dim rsql As String = ""
        rsql = " SELECT FundCenterReleases.FundCenter_FundCenterCode FROM FundCenterReleases "
        rsql += " WHERE PlanYear = " & Val(PlanYearsTxt.Text) & " "
        rsql += " AND Version = '" & VersionTxt.Text & "' "
        rsql += " AND FundCenter_FundCenterCode IN (" & prefil2 & ")" : rsql += " AND ReleaseFlag = 'Y' "
        'Response.Write(rsql)
        'Exit Sub
        Dim re_dt As New FormLockDS.FundCenterReleasesDataTable
        re_adap.GetDataByFilter(re_dt, rsql)
        For Each ritem As FormLockDS.FundCenterReleasesRow In re_dt
            lockhash.Remove(ritem.FundCenter_FundCenterCode) : lockhash.Add(ritem.FundCenter_FundCenterCode, True)
        Next
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New F05_DSTableAdapters.SumF05TableAdapter
        Dim datatable As New F05_DS.SumF05DataTable
        Dim prefil As String = ""
        prefil = Session("prefilter")

        'If IsPostBack = False Then
        '    Dim f05adap As New F05_DSTableAdapters.BudgetDetailsTableAdapter
        '    Dim f05dt As New F05_DS.BudgetDetailsDataTable
        '    Dim prex As Array = prefil.Split(",")

        '    For i = 0 To prex.Length - 1
        '        Dim no_list As New Hashtable
        '        For n = 1 To 14
        '            no_list.Add(n, n)
        '        Next

        '        f05dt = f05adap.GetListNo(prefil(i), Val(PlanYearsTxt.Text))
        '        For Each row As F05_DS.BudgetDetailsRow In f05dt
        '            no_list.Remove(Val(row.ProductItemNo))
        '        Next

        '        For Each Itemss In no_list

        '        Next
        '    Next

        'End If

        'If Session("Versions") > 1 Then
        If IsPostBack = True And Session("Search") = "true" Then
            Dim sql As String = ""
            Dim fctr As String = ""
            fctr = Session("search_fctr")
            If TabContainer1.ActiveTab.ID = TabPanel1.ID Then

                sql += " SELECT CONVERT(varchar,F05_1.FundCenter_FundCenterCode) + ',' + CONVERT(varchar,F05_1.Product_ProductCode) + ',' + CONVERT(varchar,F05_1.PlanYear) + ',' + CONVERT(varchar,F05_1.Version) + ',' + CONVERT(varchar,((ABS(F05_1.ProductItemNo) % 2) + 1)) AS Keyval  "
                sql += " , ((ABS(F05_1.ProductItemNo) % 2) + 1) AS SumID,F05_1.Cmmt_CmmtCode,F05_2.Cmmt_CmmtCode AS Cmmt_CmmtCode2 "
                sql += " , F05_1.PlanYear , F05_1.Version "
                sql += " , SUM(F05_1.Element1) AS Element1, SUM(F05_1.Element2) AS Element2, SUM(F05_1.Element3) AS Element3, SUM(F05_1.Element4) AS Element4 , SUM(F05_1.Amount) AS Amount "
                sql += " , SUM(F05_2.Element3) AS Element2_3, SUM(F05_2.Element4) AS Element2_4, SUM(F05_2.Amount) AS Amount2 "
                sql += " , F05_1.FormId , F05_1.FundCenter_FundCenterCode, F05_1.Product_ProductCode, F05_1.ProductItemDesc "
                sql += " FROM F05_1 "
                sql += " INNER JOIN F05_2 "
                sql += " ON F05_1.ProductItemNo = F05_2.ProductItemNo "
                sql += " AND F05_1.Product_ProductCode = F05_2.Product_ProductCode  "
                sql += " AND F05_1.FundCenter_FundCenterCode = F05_2.FundCenter_FundCenterCode "
                sql += " WHERE 1 = 1 "

                If hdf1.Value.ToString <> "" Then
                    sql += " AND F05_1.Product_ProductCode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += Numx(i).ToString
                        End If

                    Next
                    sql += ") "
                End If
                sql += " AND F05_1.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND F05_2.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND F05_1.Version = '" & VersionTxt.Text & "' "
                sql += " AND F05_2.Version = '" & VersionTxt.Text & "' "
                sql += " AND F05_1.FormId = 'F05' "
                sql += " AND F05_2.FormId = 'F05' "
                If fctr <> "" Then
                    If fctr <> "0" Then
                        sql += " AND F05_1.FundCenter_FundCenterCode IN (" & fctr & ") "
                        sql += " AND F05_2.FundCenter_FundCenterCode IN (" & fctr & ") "
                    Else
                        sql += " AND F05_1.FundCenter_FundCenterCode IN (" & prefil & ") "
                        sql += " AND F05_2.FundCenter_FundCenterCode IN (" & prefil & ") "
                    End If
                Else
                    sql += " AND F05_1.FundCenter_FundCenterCode IN (" & prefil & ") "
                    sql += " AND F05_2.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " GROUP BY  (ABS(F05_1.ProductItemNo) % 2 + 1) ,F05_1.Cmmt_CmmtCode,F05_2.Cmmt_CmmtCode "
                sql += " , F05_1.PlanYear, F05_1.Version , F05_1.FormId, F05_1.FundCenter_FundCenterCode , F05_1.Product_ProductCode, F05_1.ProductItemDesc  "
                sql += " ORDER BY F05_1.FundCenter_FundCenterCode,F05_1.Product_ProductCode,SumId "
                adapter.GetDataByFilter(datatable, sql)
                'Response.Write(sql)

            ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then

                sql += " SELECT CONVERT(varchar,F05_1.FundCenter_FundCenterCode) + ',' + CONVERT(varchar,F05_1.Product_ProductCode) + ',' + CONVERT(varchar,F05_1.PlanYear) + ',' + CONVERT(varchar,F05_1.Version) + ',' + CONVERT(varchar,((ABS(F05_1.ProductItemNo) % 2) + 1)) AS Keyval  "
                sql += " , ((ABS(F05_1.ProductItemNo) % 2) + 1) AS SumID,F05_1.Cmmt_CmmtCode,F05_2.Cmmt_CmmtCode AS Cmmt_CmmtCode2 "
                sql += " , F05_1.PlanYear , F05_1.Version "
                sql += " , SUM(F05_1.Element1) AS Element1, SUM(F05_1.Element2) AS Element2, SUM(F05_1.Element3) AS Element3, SUM(F05_1.Element4) AS Element4 , SUM(F05_1.Amount) AS Amount "
                sql += " , SUM(F05_2.Element3) AS Element2_3, SUM(F05_2.Element4) AS Element2_4, SUM(F05_2.Amount) AS Amount2 "
                sql += " , F05_1.FormId , F05_1.FundCenter_FundCenterCode, F05_1.Product_ProductCode, F05_1.ProductItemDesc "
                sql += " FROM F05_1 "
                sql += " INNER JOIN F05_2 "
                sql += " ON F05_1.ProductItemNo = F05_2.ProductItemNo "
                sql += " AND F05_1.Product_ProductCode = F05_2.Product_ProductCode  "
                sql += " AND F05_1.FundCenter_FundCenterCode = F05_2.FundCenter_FundCenterCode "
                sql += " WHERE 1 = 1 "

                If hdf1.Value.ToString <> "" Then
                    sql += " AND F05_1.Product_ProductCode IN ("
                    Dim Numx As Array = hdf1.Value.Split(",")
                    For i = 0 To Numx.Length - 1
                        If Val(Numx(i)) <> 0 Then
                            If i <> 0 Then
                                sql += ","
                            End If
                            sql += "'" & Numx(i).ToString & "'"
                        End If
                    Next
                    sql += ") "
                End If
                sql += " AND F05_1.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND F05_2.PlanYear = " & PlanYearsTxt.Text & " "
                sql += " AND F05_1.Version = '" & VersionTxt.Text & "' "
                sql += " AND F05_2.Version = '" & VersionTxt.Text & "' "
                sql += " AND F05_1.FormId = 'F05' "
                sql += " AND F05_2.FormId = 'F05' "
                If fctr <> "" Then
                    sql += " AND F05_1.FundCenter_FundCenterCode IN (" & fctr & ") "
                    sql += " AND F05_2.FundCenter_FundCenterCode IN (" & fctr & ") "
                Else
                    sql += " AND F05_1.FundCenter_FundCenterCode IN (" & prefil & ") "
                    sql += " AND F05_2.FundCenter_FundCenterCode IN (" & prefil & ") "
                End If
                sql += " GROUP BY  (ABS(F05_1.ProductItemNo) % 2 + 1) ,F05_1.Cmmt_CmmtCode,F05_2.Cmmt_CmmtCode "
                sql += " , F05_1.PlanYear, F05_1.Version , F05_1.FormId, F05_1.FundCenter_FundCenterCode , F05_1.Product_ProductCode, F05_1.ProductItemDesc  "
                sql += " ORDER BY F05_1.FundCenter_FundCenterCode,F05_1.Product_ProductCode,SumId "
                adapter.GetDataByFilter(datatable, sql)
                'Response.Write(sql)

            End If

            'Response.Write(sql)
            datatable.Columns("PlanYear").AllowDBNull = True
            datatable.Columns("Version").AllowDBNull = True
            datatable.Columns("FundCenter_FundCenterCode").AllowDBNull = True

            datatable.Columns("Keyval").ReadOnly = False

            'Response.Write(Val(Session("FCTR")))
            e.DataSource = datatable


            'datatable = adapter.GetDataByMultiple(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))
        Else
            Dim sql As String = ""

            sql += " SELECT CONVERT(varchar,F05_1.FundCenter_FundCenterCode) + ',' + CONVERT(varchar,F05_1.Product_ProductCode) + ',' + CONVERT(varchar,F05_1.PlanYear) + ',' + CONVERT(varchar,F05_1.Version) + ',' + CONVERT(varchar,((ABS(F05_1.ProductItemNo) % 2) + 1)) AS Keyval  "
            sql += " , ((ABS(F05_1.ProductItemNo) % 2) + 1) AS SumID,F05_1.Cmmt_CmmtCode,F05_2.Cmmt_CmmtCode AS Cmmt_CmmtCode2 "
            sql += " , F05_1.PlanYear , F05_1.Version "
            sql += " , SUM(F05_1.Element1) AS Element1, SUM(F05_1.Element2) AS Element2, SUM(F05_1.Element3) AS Element3, SUM(F05_1.Element4) AS Element4 , SUM(F05_1.Amount) AS Amount "
            sql += " , SUM(F05_2.Element3) AS Element2_3, SUM(F05_2.Element4) AS Element2_4, SUM(F05_2.Amount) AS Amount2 "
            sql += " , F05_1.FormId , F05_1.FundCenter_FundCenterCode, F05_1.Product_ProductCode, F05_1.ProductItemDesc "
            sql += " FROM F05_1 "
            sql += " INNER JOIN F05_2 "
            sql += " ON F05_1.ProductItemNo = F05_2.ProductItemNo "
            sql += " AND F05_1.Product_ProductCode = F05_2.Product_ProductCode  "
            sql += " AND F05_1.FundCenter_FundCenterCode = F05_2.FundCenter_FundCenterCode "
            sql += " WHERE 1 = 1 "

            sql += " AND F05_1.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND F05_2.PlanYear = " & PlanYearsTxt.Text & " "
            sql += " AND F05_1.Version = '" & VersionTxt.Text & "' "
            sql += " AND F05_2.Version = '" & VersionTxt.Text & "' "
            sql += " AND F05_1.FormId = 'F05' "
            sql += " AND F05_2.FormId = 'F05' "
            If prefil <> "" Then
                sql += " AND F05_1.FundCenter_FundCenterCode IN (" & prefil & ") "
                sql += " AND F05_2.FundCenter_FundCenterCode IN (" & prefil & ") "
            End If
            sql += " GROUP BY  (ABS(F05_1.ProductItemNo) % 2 + 1) ,F05_1.Cmmt_CmmtCode,F05_2.Cmmt_CmmtCode "
            sql += " , F05_1.PlanYear, F05_1.Version , F05_1.FormId, F05_1.FundCenter_FundCenterCode , F05_1.Product_ProductCode, F05_1.ProductItemDesc  "
            sql += " ORDER BY F05_1.FundCenter_FundCenterCode,F05_1.Product_ProductCode,SumId "
            adapter.GetDataByFilter(datatable, sql)
            'Response.Write(sql)
            'datatable = adapter.GetDataByMultiple2(Val(Session("PlanYear")), Val(Session("Versions")), Session("users"))

            datatable.Columns("PlanYear").AllowDBNull = True
            datatable.Columns("Version").AllowDBNull = True
            datatable.Columns("FundCenter_FundCenterCode").AllowDBNull = True
            'datatable.Columns("FundCenter_PlanYear").AllowDBNull = True

            datatable.Columns("Keyval").ReadOnly = False

            e.DataSource = datatable

        End If

    End Sub
    Dim old_fctr As String = ""
    Dim old_prod As String = ""
    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        If e.Row.Type = RowType.Record Then
            If lockhash(e.Row.Cells(2).Value.ToString.Trim) = True Then
                e.Row.Cells(0).Column.IsRowChecker = False
                e.Row.ForceNoEdit = True
                e.Row.Style.BackColor = Drawing.Color.Gray
            Else
                e.Row.Cells(0).Column.IsRowChecker = True
            End If

        End If

        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(2).ForceNoEdit = True
        e.Row.Cells(3).ForceNoEdit = True
        e.Row.Cells(4).ForceNoEdit = True
        If e.Row.Type = RowType.Record Then
            Dim dr As DataRowView = DirectCast(e.Row.DataRow, DataRowView)
            Dim code As String = dr("SumId").ToString()
            If code = "2" Then
                e.Row.Style.BorderSettings.Bottom.Width = 3
                e.Row.Style.BorderSettings.Bottom.Style = BorderStyle.Solid
                e.Row.Style.BorderSettings.Bottom.Color = Drawing.Color.DarkGray
            End If

            Dim fctr As String = dr("FundCenter_FundCenterCode").ToString()
            Dim prod As String = dr("Product_ProductCode").ToString()
            If fctr = old_fctr And prod = old_prod Then
                e.Row.Cells(2).Text = ""
                e.Row.Cells(2).Value = ""
                e.Row.Cells(3).Text = ""
                e.Row.Cells(3).Value = ""
            End If
            old_fctr = fctr
            old_prod = prod
        End If

    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)



    End Sub


    Sub PlotNode()

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()

        Dim adapter As New FundCenterParentTableAdapters.fctr_viewTableAdapter
        Dim dt As New FundCenterParent.fctr_viewDataTable
        dt = adapter.GetDataByUser(Session("users"), Val(PlanYearsTxt.Text))

        Dim root_id As String

        MyNode.Text = "GSB"
        MyNode.Value = "0"
        root_id = "0"

        If dt.Count > 0 Then

            WebTreeView3.Nodes.Add(MyNode)

            Dim rootpath = ""
            Dim prefil As String = ""
            prefil = Session("prefilter")
            rootpath = FctrClass.GetRootPrefil(Session("users"), Val(Session("planyear")), prefil)
            Dim rootarr() As String = Split(rootpath, ",")
            WebTreeView3.Nodes.Add(MyNode)

            For i = 0 To rootarr.Length - 1
                PlotChild(rootarr(i), MyNode, 1)
            Next
        End If

    End Sub

    Sub PlotChild(ByVal gpath As String, ByRef cnode As WebTreeViewNode, ByVal level As Decimal)
        'Response.Write(gpath & "<br>")
        Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHDataTable
        Hdatatable = Hadapter.GetDataByGPath(gpath, Val(Session("planyear")))

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()
        For Each row As FundCenterParent.FctHRow In Hdatatable


            MyNode.Text = row.Description
            MyNode.Value = row.FundCenter_FundCenterCode

            cnode.Nodes.Add(MyNode)
        Next


        If Len(gpath) >= 10 Then
            Exit Sub
        End If

        Dim hadap2 As New FundCenterParentTableAdapters.UserLikeGpathTableAdapter
        Dim hdt2 As New FundCenterParent.UserLikeGpathDataTable
        Dim sql As String = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")

        Dim pathadd As String = "__"
        'For i = 1 To 4
        sql = ""
        Dim fullpath As String = gpath & pathadd
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " AND grouppath like '" & fullpath & "' "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND Fcth.PlanYear = " & Val(Session("PlanYear"))
        hadap2.GetDataByFilter(hdt2, sql)

        For Each row As FundCenterParent.UserLikeGpathRow In hdt2
            If Len(row.grouppath) <= 10 Then
                PlotChild(row.grouppath, MyNode, level + 1)
            End If

        Next

    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        GenerateValues()
        If TabContainer1.ActiveTab.ID = TabPanel1.ID Then
            Session.Remove("search_fctr")
            If WebCombo1.Value <> "" And WebCombo2.Value <> "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo2.Value, Session("users"), Val(PlanYearsTxt.Text)))
            ElseIf WebCombo1.Value <> "" And WebCombo2.Value = "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo1.Value, Session("users"), Val(PlanYearsTxt.Text)))
                'Response.Write(FctrClass.GetFromToFundCenter(WebCombo1.Value, WebCombo1.Value, Session("users")))
            ElseIf WebCombo1.Value = "" And WebCombo2.Value <> "" Then
                Session.Add("search_fctr", FctrClass.GetFromToFundCenter(WebCombo2.Value, WebCombo2.Value, Session("users"), Val(PlanYearsTxt.Text)))
                'Response.Write(FctrClass.GetFromToFundCenter(WebCombo2.Value, WebCombo2.Value, Session("users")))
            Else
                Session.Add("search_fctr", Session("prefilter"))
            End If

        ElseIf TabContainer1.ActiveTab.ID = TabPanel2.ID Then
            Session.Remove("search_fctr")
            If WebCombo3.Value = "" Then
                Session.Add("search_fctr", Session("prefilter"))
            Else
                Session.Add("search_fctr", FctrClass.GetSubFundCenter(WebCombo3.Value, Session("users"), Val(PlanYearsTxt.Text), Session("prefilter")))
            End If

        End If
        Session.Remove("Search")
        Session.Add("Search", "true")
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Sub GenerateValues()
        If hdf1.Value.Length = 0 Then
            Session("Search") = "false"
        End If
        Try
            Dim Numx As Array = hdf1.Value.Split(",")
            For i = 0 To Numx.Length - 1
                ms8.Items.FindByValue(Numx(i).ToString).Selected = True
            Next
        Catch ex As Exception
            logger.Error(ex.Message)
            logger.Error(ex.StackTrace)
        End Try

    End Sub


    'Protected Sub coppy_btn_Click(sender As Object, e As ImageClickEventArgs) Handles coppy_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.CoppyRow(keyValue)

    '    Next keyValue

    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub

    'Protected Sub delete_btn_Click(sender As Object, e As ImageClickEventArgs) Handles delete_btn.Click

    '    Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

    '    Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

    '    For Each keyValue As String In checkedRows

    '        adapter.DeleteRow(keyValue)

    '    Next keyValue



    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()

    'End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim ActionType As String = TryCast(Request("type"), String)
            If ActionType = "1" Then
                'Dim checkedRows As ArrayList = WebGrid1.RootTable.GetCheckedRows()

                'Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter

                'For Each keyValue As String In checkedRows

                '    adapter.CoppyRow(keyValue)

                'Next keyValue
            Else
                Dim adapter As New fundCenterTableAdapters.BudgetDetailsTableAdapter
                Dim adapter2 As New fundCenterTableAdapters.AttachmentsTableAdapter

                Dim f05adap As New F05_DSTableAdapters.BudgetDetailsTableAdapter
                Dim f05dt As New F05_DS.BudgetDetailsDataTable
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows
                    Dim keystring = keyValue.Split(",")
                    Dim fctr As String = keystring(0)
                    Dim prod As String = keystring(1)
                    Dim pyear As String = keystring(2)
                    Dim vers As String = keystring(3)
                    'WebGrid1.ClientAction.Alert(fctr & prod & pyear & vers)
                    f05dt = f05adap.GetIDF05(fctr, prod, Val(pyear), vers, "F05")
                    For Each item As F05_DS.BudgetDetailsRow In f05dt
                        'WebGrid1.ClientAction.Alert(item.Id)
                        Try
                            AttachClass.DeleteSP(item.Id)
                        Catch ex As Exception

                        End Try
                        adapter2.DeleteAttach(item.Id)
                        adapter.DeleteRow(item.Id)
                    Next

                    logger.Info("User " & Session("users") & " delete product " & prod & " of fundcenter " & fctr)
                Next keyValue
            End If

            WebGrid1.ClientAction.Refresh()
        End If

    End Sub

    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo1.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt




        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub WebCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo2.InitializeDataSource
        'If Session("Versions") > 1 Then
        'ds = PublicFunction.GetChild2(Session("users"))
        'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
        'e.DataSource = ds
        Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
        Dim dt As New BasicData.FCTR_Child2DataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FundCenters.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += "ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)
        dt.Rows.Add("", "ทุกรายการ", "1", "0")
        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt



        'Else
        'Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        'Dim dt As New BasicData.FCTR_ChildDataTable
        'dt = adap.GetOwnData(Session("FCTR"))
        'e.DataSource = dt
        'End If
    End Sub

    Protected Sub WebGrid1_InitializeColumn(sender As Object, e As ColumnEventArgs) Handles WebGrid1.InitializeColumn
        If e.Column.ColumnType = ColumnType.CheckBox And lockhash.Count > 0 Then
            e.Column.IsRowChecker = False
            e.Column.Width = 24
            e.Column.Caption = " "
        End If
    End Sub

    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo3.InitializeDataSource

        Dim adap As New FundCenterParentTableAdapters.SelectorTableAdapter
        Dim dt As New FundCenterParent.SelectorDataTable
        Dim dt2 As New FundCenterParent.SelectorDataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += "ORDER BY Grouppath"
        adap.GetDataByFilter(dt2, sql)
        dt.Rows.Add("ทุกรายการ", "0", 0, "0", Val(Session("PlanYear")), "ทุกรายการ")
        dt.Merge(dt2)
        'dt.DefaultView.Sort = "FundCenter_FundCenterCode"
        e.DataSource = dt



    End Sub
End Class
